// pagesEnterprise/pages/recruitment/recruitment.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      search: '',
      pageSize: 10,
      pageNum: 1
    },
    userInfo: null,
    jobsList: []
  },
  /**
   * 发布岗位事件
   */
  publishJobEvent() {
    
    const userInfo = this.data.userInfo
    if (!userInfo) return ajax.showToast('前往登录');

    const roles = userInfo.roles || null;

    if (!roles || roles.company != 1) return wx.showModal({
      title: '提示',
      content: '入驻企业才能发布招聘信息, 是否入驻',
      success: (res) => {
        console.log(res)
        if (!res.confirm) return;
        wx.navigateTo({ url: '/pagesEnterprise/pages/enterpriseSign/enterpriseSign' })
      }
    })

    wx.navigateTo({ url: '/pagesEnterprise/pages/publishJob/publishJob' })
  },

  /**
   * input 变化事件
   */
  searchChange(event) {
    this.data.formData.search = event.detail.value
    this.searchConfirm()
  },
  /**
   * 搜索事件
   */
  searchConfirm(event) {
    this.data.formData.pageNum = 1;
    this.getRequestList()
  },
  /**
   * 获取招聘列表数据
   */
  async getRequestList() {
    ajax.showLoading()
    const { formData, jobsList } = this.data
    const res = await ajax.get('/enterpriseservice/recruit/jobs/list', formData).finally(() => (wx.hideLoading(), wx.stopPullDownRefresh()));
    const list = formData.pageNum == 1 ? res.rows : jobsList.concat(res.rows)
    this.setData({ jobsList: list })
  },
  /**
   * 获取用户信息
   */
  async getUserInfo () {
    const res = await ajax.getUserInfo()
    this.data.userInfo = res || wx.getStorageSync('userInfo')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRequestList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.formData.pageNum = 1;
    this.getRequestList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.formData.pageNum += 1;
    this.getRequestList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})